/*
 * @Author: 生俊甫 1758142861@qq.com
 * @Date: 2024-06-07 16:00:56
 * @LastEditors: 生俊甫 1758142861@qq.com
 * @LastEditTime: 2024-06-12 18:39:50
 * @FilePath: /sjf/2024-project/2024_centos/source/test.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <iostream>
#include "util.hpp"
#include "./db/db.hpp"
#include "./log/log.hpp"
#include "./user.hpp"
#include "./room.hpp"
#include "./session.hpp"
#include "./matcher.hpp"
#include "./log/logger.hpp"

const std::string host = "localhost";//IP
const std::string user = "root";//用户名
const std::string passwd = "123456";//密码
const std::string db = "gobang1";//连接对应数据库
const unsigned int port = 3306;//数据库服务端口号

bool mysql_test()
{
    MYSQL* mysql = util_mysql::mysql_create(host,user,passwd,db,port);
    std::string str = "insert into stu values(null,'赵六',19,100,100,100)";
    if(util_mysql::mysql_exec(mysql,str) == false)
    {
        ERR_LOG("mysql insert err");
        return -1;
    }
    util_mysql::mysql_destory(mysql);
    return true;
}

void json_test()
{
    Json::Value root;
    root["姓名"] = "张三";
    root["年龄"] = 22;
    root["成绩"].append(99);
    root["成绩"].append(89);
    root["成绩"].append(95.5);
    std::string str;
    util_json::serialize(root,str);
    DBG_LOG("serial: %s",str.c_str());

    Json::Value un_root;
    util_json::unserialize(str,un_root);
    std::cout << "姓名: " << un_root["姓名"].asString() << std::endl;
    std::cout << "年龄: " << un_root["年龄"].asInt() << std::endl;
    int size = un_root["成绩"].size();
    for(int i = 0; i < size; i++)
    {
        std::cout << "成绩: " << un_root["成绩"][i].asString() << std::endl;
    }

}

void db_test()
{
    user_table ut(host,user,passwd,db,port);
    // Json::Value user;
    // user["username"] = "wangwu";
    // user["password"] = "123";
    // ut.user_login(user);

    Json::Value user;
    ut.select_user_by_name("zhangsan",user);
    std::cout << user["score"].asInt64() << std::endl;
    // std::string str;
    // util_json::serialize(user,str);
    // std::cout << str << std::endl;

    // ut.win(1);
    // ut.select_user_by_name("zhangsan",user);
    // std::string str1;
    // util_json::serialize(user,str1);
    // std::cout << str1 << std::endl;

    // ut.lose(1);
    // ut.select_user_by_name("zhangsan",user);
    // std::string str2;
    // util_json::serialize(user,str2);
    // std::cout << str2 << std::endl;

    // Json::Value un_user;
    // std::string un_str;
    // util_json::unserialize(str2,un_user);
    // std::cout << un_user["id"].asInt() << std::endl;
    // std::cout << un_user["username"].asCString() << std::endl;
}

typedef websocketpp::server<websocketpp::config::asio> wsserver_t;
void online_test()
{
    online_user user;
    wsserver_t::connection_ptr conn;
    user.get_game_hall(1,conn);
    if(user.user_in_hall(1))
        DBG_LOG("user in hall");
    else   
        DBG_LOG("user no hall");
    user.get_game_room(1,conn);
    if(user.user_in_hall(1))
        DBG_LOG("user in hall");
    else   
        DBG_LOG("user no hall");
    if(user.user_in_room(1))
        DBG_LOG("user in room");
    else   
        DBG_LOG("user no room");
    user.exit_game_room(1);
    if(user.user_in_room(1))
        DBG_LOG("user in room");
    else   
        DBG_LOG("user no room");
}

void room_test()
{
    user_table ut(host,user,passwd,db,port);
    online_user ou;
    wsserver_t::connection_ptr conn;
    ou.get_game_hall(1,conn);
    ou.get_game_hall(2,conn);

    // room rm(1,&ou,&ut);
    room_manage rm(&ut,&ou);
    room_ptr rp = rm.create_room(1,2);
}

using session_ptr = std::shared_ptr<session>;
void session_test()
{
    wsserver_t* ser;
    session_manager sm(ser);
    session_ptr sp = sm.create_session(1,LOGIN);
}

void matcher_tesr()
{
    user_table ut(host,user,passwd,db,port);
    online_user ou;
    room_manage rm(&ut,&ou);
    matcher_manager mm(&ut,&ou,&rm);
}

int main()
{
    //mysql_test();
    //json_test();
    //db_test();
    //online_test();
    //room_test();
    //session_test();
    matcher_tesr();
    logMessage(1,"666");
    return 0;
}